<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class city extends core {

	public function __construct() {
		parent::__construct();
	}

	//列表
	public function index() {
		$where = '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$city_id = (isset($_GET['city_id'])) ? trim($_GET['city_id']) : '';

		if ($keyword) {
			$where .= "AND (name LIKE '%{$keyword}%')";
		}
		if ($city_id) {
			$where .= "AND city_id='{$city_id}'";
		}
		//分页
		$this->ezr->num_results_per_page = 20;
		$this->setpages(array('city_id'=>$city_id, 'keyword'=>$keyword));

		$sql = "SELECT * FROM {$this->tbp}open_city WHERE 1 {$where} ORDER BY id DESC";
		//echo $sql;
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('city_id',$city_id);
		$this->smarty->assign('page',$nav);

		$this->display();
	}

	//编辑
	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$this->display();
		}
	}

	//编辑
	public function edit() {
		if (IS_POST) {
			$this->save();
		} else {
			$id = (isset($_GET['id']) && intval($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM {$this->tbp}open_city WHERE id='{$id}'";
			$city = $this->db->get_row($sql);
			$this->smarty->assign('row', $city);
			$this->display();
		}
	}

	//保存
	public function save() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$city_id = (isset($_POST['city_id']) && intval($_POST['city_id'])) ? intval($_POST['city_id']) : 0;
		$province_id = (isset($_POST['province_id']) && intval($_POST['province_id'])) ? intval($_POST['province_id']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
		$pinyin = (isset($_POST['pinyin']) && trim($_POST['pinyin'])) ? strtoupper(trim($_POST['pinyin'])) : '';
		$first_letter = (isset($_POST['first_letter']) && trim($_POST['first_letter'])) ? strtoupper(trim($_POST['first_letter'])) : '';
		$is_hot = (isset($_POST['is_hot']) && intval($_POST['is_hot'])) ? intval($_POST['is_hot']) : 0;
		$opening = (isset($_POST['opening']) && intval($_POST['opening'])) ? intval($_POST['opening']) : 0;
		$sort = (isset($_POST['sort']) && intval($_POST['sort'])) ? intval($_POST['sort']) : 999;

		if ($id>0) {
			$sql = "UPDATE {$this->tbp}open_city SET name='{$name}', pinyin='{$pinyin}', is_hot='{$is_hot}', opening='{$opening}',
				sort='{$sort}', first_letter='{$first_letter}', city_id='{$city_id}' , province_id='{$province_id}'
				WHERE id='{$id}'";
			$this->db->query($sql);
		} else {
			$sql = "SELECT COUNT(*) FROM {$this->tbp}open_city WHERE name='{$name}'";
			if (intval($this->db->get_var($sql))) error('该城市已经存在');
			$sql = "INSERT INTO {$this->tbp}open_city(name, pinyin, is_hot, sort, first_letter, city_id, province_id, opening)
				VALUES('{$name}', '{$pinyin}', '{$is_hot}', '{$sort}', '{$first_letter}', '{$city_id}', '{$province_id}', '{$opening}')";
			$this->db->query($sql);
			//echo $sql;
			//exit;
			$city_id = $this->db->insert_id;
		}
		//echo $sql;
		//echo $city_id;
		//exit;
		header("Location:?app=city&act=index");
	}

	//删除城市
	public function delete() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM {$this->tbp}open_city WHERE id='{$id}'";
		$this->db->query($sql);
		header("Location:?app=city&act=index");
	}

	//根据名称查找城市、区域编号
	public function get_city_code() {
		$where = '';
		$city = (isset($_GET['city']) && trim($_GET['city'])) ? trim($_GET['city']) : '';
		$city_id = (isset($_GET['city_id'])) ? trim($_GET['city_id']) : '';

		if ($city) {
			$where .= "AND (name LIKE '%{$city}%')";
		}
		//分页
		$this->ezr->num_results_per_page = 20;
		$this->setpages(array('city'=>$city));

		$sql = "SELECT * FROM {$this->tbp}city WHERE 1 {$where} ORDER BY id DESC";
		//echo $sql;
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();

		$this->smarty->assign('rs', $rs);
		$this->smarty->assign('page',$nav);
		$this->display();
	}

	//根据名称查找城市、区域编号
	public function get_district_code() {
		$where = '';
		$district = (isset($_GET['district']) && trim($_GET['district'])) ? trim($_GET['district']) : '';

		if ($district) {
			$where .= "AND (d.name LIKE '%{$district}%')";
		}
		//分页
		$this->ezr->num_results_per_page = 20;
		$this->setpages(array('district'=>$district));

		$sql = "SELECT d.*, c.name as city_name FROM {$this->tbp}district d LEFT JOIN im_city c ON d.father_id=c.city_id WHERE 1 {$where} ORDER BY d.id DESC";
		//echo $sql;
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();

		$this->smarty->assign('rs', $rs);
		$this->smarty->assign('page',$nav);
		$this->display();
	}
}
